package com.cloudsigns.factorymanager2.project.customview;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;

import com.cloudsigns.factorymanager2.project.myinerface.IEditTextChangeListener;

/**
 * Created by tony on 2017/12/1.
 */

public class EditTextWatcher implements TextWatcher {
    IEditTextChangeListener editTextChangeListener;
    EditText[] editTexts;

    public void setEditTextChangeListener(IEditTextChangeListener editTextChangeListener) {
        this.editTextChangeListener = editTextChangeListener;
    }

    public void addAllEditText(EditText... editTexts) {
        this.editTexts = editTexts;
        initEditListener();
    }

    private void initEditListener() {
        for (EditText editText : editTexts) {
            editText.addTextChangedListener(this);
        }
    }


    /**
     * 检查所有的edit是否输入了数据
     *
     * @return
     */
    private boolean checkAllEdit() {
        for (EditText editText : editTexts) {
            if (!TextUtils.isEmpty(editText.getText() + "")) {
                continue;
            } else {
                return false;
            }
        }
        return true;
    }


    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if (checkAllEdit())

            editTextChangeListener.onTextChange(true);
        else

            editTextChangeListener.onTextChange(false);
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
}
